<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px" class="queryForms">
			<el-form-item label="文件名称" prop="fileName">
				<el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="接收人" prop="userId">
				<el-select v-model="queryParams.userId" placeholder="请选择接收人" filterable>
					<el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
				</el-select>
			</el-form-item>
			<el-form-item label="接收日期" prop="receiveTime">
				<el-date-picker v-model="queryParams.receiveTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择接收日期"> </el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['filercv:filercvlist:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['filercv:filercvlist:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button size="mini" type="info" @click="handleBacks">返回</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="rcvlist16" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="filercv:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="tableList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="文件名称" prop="fileName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="文件编号" prop="fileNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="主要内容摘要" align="left" prop="contents" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="发文单位" prop="deptName" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="接收人" align="center" prop="userName" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="接收日期" align="center" prop="receiveTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="文件类型" align="center" prop="fileTypeId" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.file_type" :value="scope.row.fileTypeId" />
					</template>
				</el-table-column>

				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="180" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['filercv:filercvlist:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['filercv:filercvlist:remove']">删除</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleAnnex(scope.row)" v-hasPermi="['filercv:filercvlist:annex']">附件</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination19" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改文件接收台账对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px" class="add-form2">
				<el-form-item label="文件名称" prop="fileName">
					<el-input v-model="form.fileName" placeholder="请输入文件名称" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-form-item label="文件编号" prop="fileNumber">
					<el-input v-model="form.fileNumber" placeholder="请输入文件编号" maxlength="20" show-word-limit  />
				</el-form-item>

				<el-form-item label="文件类型" prop="fileTypeId">
					<el-select v-model="form.fileTypeId" placeholder="请选择文件类型" disabled>
						<el-option v-for="dict in dict.type.file_type" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="发文单位" prop="deptId">
					<el-cascader ref="deptref" placeholder="请选择发文单位" v-model="form.deptId" :options="deptList" :props="{ expandTrigger: 'hover', value: 'id', emitPath: false }" @change="changeDept()"></el-cascader>
				</el-form-item>
				<el-form-item label="接收人" prop="userId">
					<el-select v-model="form.userId" placeholder="请选择接收人" @change="changeUser()" filterable>
						<el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
					</el-select>
				</el-form-item>
				<el-form-item label="接收时间" prop="receiveTime">
					<el-date-picker v-model="form.receiveTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择购置日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="主要内容摘要" style="width: 100%" prop="contents">
					<el-input type="textarea" v-model="form.contents" placeholder="请输入主要内容摘要" maxlength="100" show-word-limit  />
				</el-form-item>
				<el-form-item label="附件" prop="tfileStandingFileslist">
					<FileUpload @input="getFiles" :value="uploadList"></FileUpload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listFile, getFile, delFile, addFile, updateFile } from "@/api/filercv/rcvlist"
import { deptTreeSelect } from "@/api/system/user"
import { getUserList ,getcolumn} from "@/api/selectList"
import FileUpload from "@/components/FileUpload"
import { mapGetters } from "vuex"
export default {
	name: "rcvlistList",
	dicts: ["file_type"],
	components: { FileUpload },
	data() {
		return {
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 文件接收台账表格数据
			tableList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				fileName: undefined,
				userId: undefined,
				receiveTime: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				auxiliaryConsumablesName: [{ required: true, message: "请输入文件接收台账名称", trigger: "blur" }]
			},
			// 文件接收台账类型列表
			auxiliaryTypes: [],
			deptList: [],
			userList: [],
			uploadList: [],
			// 列信息
			columns: [
				{ key: 0, label: `文件名称`, visible: true },
				{ key: 1, label: `文件编号`, visible: true },
				{ key: 2, label: `主要内容摘要`, visible: true },
				{ key: 3, label: `发文单位`, visible: true },
				{ key: 4, label: `接收人`, visible: true },
				{ key: 5, label: `接收日期`, visible: true },
				{ key: 6, label: `文件类型`, visible: true }
			]
		}
	},
	created() {
		// this.getSize()
		// this.getPullDownlist()
	},
	computed: {
		...mapGetters(["sidebar"])
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		this.getPullDownlist()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination19" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		handleBacks() {
			// 关闭当前tab页签，打开新页签
			const obj = { path: "/filercv/rcvlist" }
			this.$tab.closeOpenPage(obj)
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		// 获取用户下拉列表
		async getUserList() {
			let ret = await getUserList()
			this.userList = ret.rows
		},
		getPullDownlist() {
			// 部门列表
			this.getdeptlist()
			// 用户列表
			this.getUserList()
		},
		/** 查询文件接收台账列表 */
		getList() {
			this.loading = true
			this.queryParams.fileTypeId = this.$route.query.id
			listFile(this.queryParams).then(response => {
				this.tableList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 部门列表
		getdeptlist() {
			deptTreeSelect().then(ret => {
				this.deptList = ret.data
			})
		},
		// 更改用户
		changeUser() {
			let cur = this.userList.find(ele => {
				return ele.userId == this.form.userId
			})
			this.form.userName = cur.nickName
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: undefined,
				fileName: undefined,
				fileNumber: undefined,
				fileTypeId: this.$route.query.id,
				deptId: undefined,
				userId: undefined,
				receiveTime: undefined,
				contents: undefined
			}
			this.uploadList = []
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加文件接收台账"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getFile(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改文件接收台账"
				this.uploadList = response.data.tfileStandingFileslist
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					this.form.tfileStandingFileslist = this.uploadList
					if (this.form.id != undefined) {
						updateFile(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addFile(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			console.log(ids)
			this.$modal
				.confirm("是否确认删除文件接收台账的数据项？")
				.then(function () {
					return delFile(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		// 更改部门
		changeDept() {
			this.form.deptName = this.$refs.deptref.getCheckedNodes()[0].label
		},
		// 上传
		getFiles(files) {
			this.uploadList = files
		},
		// 跳转到附件
		handleAnnex(row) {
			let url = "/filercv/filercvlist?id=" + row.id
			this.$router.push(url)
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 180px !important;
}
</style>
